$(function(){
	usermanager_onload.begin();
});

user_manager = {
		add : function(){
			var u_name = $("#uname").val();
			u_name = $.trim(u_name);
			var passwd = $("#passwd").val();
			passwd = $.trim(passwd);
			var con_passwd = $("#con_passwd").val();
			con_passwd = $.trim(con_passwd);
			var depart = $("#depart").val();
			var desc = $("#desc").val();
			var phone = $("#phone").val();
			
			if(''== u_name || u_name.length<2){
				alert('用户名不能是空,用户名不能小于2位');
				return;
			}
			
			if(''==passwd || passwd.length<6){
				alert('密码不能是空,密码长度不能够小于6位');
				return;
			}
			
			if(passwd != con_passwd){
				alert('密码与确认密码不一致');
				return;
			}
			var value = u_name+" "+passwd; 
			var key = $.cookie("k");
			value = user_manager.encrypt(value,key);
			$("#uname").val('');
			$("#passwd").val('');
			$("#con_passwd").val('');
			var auth_list = $("input[name=auth_box]");
			var permission = '';
			var list_length = auth_list.length;
			for(i=0;i<list_length;i++){
				if(auth_list[i].checked==true){
					var temp = auth_list[i].value;
					permission = permission+temp+',';
				}
			}
			
			permission = permission.substr(0,permission.length-1);
			
			$.ajax( {
				data : 'value='+value+'&permission='+permission+"&department="+depart+"&description="+desc+"&mobile_phone="+phone,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/user!register",
				success : function(data) {
					if('ok' == data.state){
						alert('添加用户成功');
						//window.location.href="http://auth.store.com/";
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
		},
		encrypt : function (word,key){
	    	 var key = CryptoJS.enc.Utf8.parse(key);    
	         var iv  = CryptoJS.enc.Utf8.parse('0102030405060755');    
	         var srcs = CryptoJS.enc.Utf8.parse(word);   
	         var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC});   
	         return encrypted.toString();   
    	},
    	select : function(){
			$("#add_user").hide();
			$("#update_user").hide();
			$("#user_table").html('');
			$("#select_user").show();
			$.ajax( {
				data : 'start='+0+"&limit="+50+'&isopen=-1',
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/user!getUserList",
				success : function(data) {
					if('ok' == data.state){
						var list = data.user_list;
						var count = data.totalCount; 
						$("#total").html('共 '+count+' 名用户');
						var temp =  '<tr>'+
                            '<th width="15%" scope="col">用户名</th>'+
                            '<th width="15%" scope="col">部门</th>'+
                             '<th width="17%" scope="col">注册时间</th>'+
                              '<th width="17%" scope="col">最后一次登陆时间</th>'+
                              '<th width="15%" scope="col">电话</th>'+ 
                              '<th width="6%" scope="col">状态</th>'+                          
                           '<th width="15%" scope="col">操作</th>'+
                          '</tr>';
                          $("#user_table").append(temp);
						for(i=0;i<count;i++){
							var uid = list[i].user_id;
							var username = list[i].username;
							var department = list[i].department==null ? '空' : list[i].department;
							var register_date = list[i].register_date;
							var last_login_date = list[i].last_login_date;
							var mobile_phone = list[i].mobile_phone==null ? '空' : list[i].mobile_phone;
							var isopen = list[i].isopen;
							var state = isopen==1 ? '正常' : '禁止';
							//var aid = list[i].id;
							var child = '<tr id=uid_'+uid+' >'+
                            '<td align="center">'+username+'</td>'+
                            '<td align="center">'+department+'</td>'+
                            '<td align="center">'+register_date+'</td>'+
                            '<td align="center">'+last_login_date+'</td>'+
                            '<td align="center">'+mobile_phone+'</td>'+
                             '<td align="center">'+state+'</td>'+
                            '<td align="center">'+
                            '<a href="javascript:user_manager.updateshow('+uid+')">修改</a>'+
                            '<a href="javascript:user_manager.remove('+uid+')">删除</a>';
                            if(1==isopen){
                            	child = child + '<a href="javascript:user_manager.setoff('+uid+')">禁止</a>'+
                          						'</tr>';
                            }else{
                            	child = child + '<a href="javascript:user_manager.seton('+uid+')">解禁</a>'+
                          						'</tr>';
                            }
                            
                          	$("#user_table").append(child);
						}
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				},
				error :  function(a,b,c) {
					var text = a.responseText;
					text = text.substr(0,15);
					var regex =/login/;
					var regex2 =/no_permission/;
					var b = regex.test(text);
					if(b){
						window.location.href=global_parameter.login_page;
					}else{
						var b2 = regex2.test(text);
						if(b2){
							window.location.href=global_parameter.login_page+"/no_permission.html";
						}
					}
				}
			});
			
		},
		addshow : function() {
			$("#add_user").show();
			$("#select_user").hide();
			$("#update_user").hide();
		},
		remove : function(id){
			$.ajax( {
				data : 'uid='+id,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/user!delete",
				success : function(data) {
					if('ok' == data.state){
						alert('删除用户成功');
						$("#uid_"+id).remove();
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
		},
		setoff : function(id){
			$.ajax( {
				data : 'uid='+id,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/user!setOff",
				success : function(data) {
					if('ok' == data.state){
						alert('禁止用户成功');
						user_manager.select();
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
		},
		seton : function(id){
			$.ajax( {
				data : 'uid='+id,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/user!setOn",
				success : function(data) {
					if('ok' == data.state){
						alert('解禁用户成功');
						user_manager.select();
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
		},
		updateshow : function(id) {
			$("#update_user").show();
			$("#select_user").hide();
			$("#add_user").hide();
			user_manager.select_auth('auth_list_update','box_update');
			var user ; 
			$.ajax( {
				data : 'uid='+id,
				dataType : "json",
				type : "POST",
				async : false,
				url : "/auth/user!getUserById",
				success : function(data) {
					if('ok' == data.state){
						user = data.user;
						var c = user.username;
						$("#update_uname").val(c);
						$("#old_uname").val(c);
						c = user.department;
						$("#update_depart").val(c);
						c = user.mobile_phone;
						$("#update_phone").val(c);
						c = user.description;
						$("#update_desc").val(c);
						$("#update_uid").val(id);
						
						
						$("#update_passwd").val("");
						$("#update_con_passwd").val("");
						var auth_list = $("input[name=box_update]");
						var list_length = auth_list.length;
						var permission = user.permission;
						var arr = permission.split(",");
						var temp ;
					
						for(j=0;j<arr.length;j++){
							temp = arr[j];
							for(i=0;i<list_length;i++){
								if(auth_list[i].value==temp){
									auth_list[i].checked=true;
								}
							}
						}
					}else{
						alert(data.msg);
					}
				} 
			});
			
			
			
			
		},
		update : function() {
	
			var uname = $("#update_uname").val();
			uname = $.trim(uname);
			var old_uname = $("#old_uname").val();
			var passwd = $("#update_passwd").val();
			passwd = $.trim(passwd);
			var con_passwd = $("#update_con_passwd").val();
			con_passwd = $.trim(con_passwd);
			var depart = $("#update_depart").val();
			var phone = $("#update_phone").val();
			var desc = $("#update_desc").val();
			
			var update_uid =  $("#update_uid").val();
			if(uname==old_uname){
				uname = "";
			}else{
				var len = uname.length;
				if(len<2){
					alert('用户名不能小于2位');
					return;
				}
			}
			
			if(''!=passwd && passwd.length<6){
				alert('密码长度不能够小于6位');
				return;
			}
			
			if(passwd != con_passwd){
				alert('密码与确认密码不一致');
				return;
			}
			
			var key = $.cookie("k");
			
			if(''!=uname){
				uname = user_manager.encrypt(uname,key);
			}
			
			if(''!=passwd){
				passwd = user_manager.encrypt(passwd,key);
				$("#update_passwd").val('');
				$("#update_con_passwd").val('');
			}
		
			var auth_list = $("input[name=box_update]");
			var permission = '';
			var list_length = auth_list.length;
			for(i=0;i<list_length;i++){
				if(auth_list[i].checked==true){
					var temp = auth_list[i].value;
					permission = permission+temp+',';
				}
			}
			
			permission = permission.substr(0,permission.length-1);
			
			$.ajax( {
				data : 'uid='+update_uid+'&username='+uname+'&password='+passwd+'&permission='+permission+"&department="+depart+"&description="+desc+"&mobile_phone="+phone,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/user!updateUserInfo",
				success : function(data) {
					if('ok' == data.state){
						alert('修改用户成功');
						user_manager.updateshow(update_uid);
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
		},
		select_auth : function(div_id,box_name){
			$.ajax( {
				data : 'start='+0+"&limit="+50+"&state="+1,
				dataType : "json",
				type : "POST",
				async : false,
				url : "/auth/authority!getAuthList",
				success : function(data) {
					if('ok' == data.state){
						//alert('获取权限列表成功');
						$("#"+div_id).html('');
						var list = data.auth_list;
						var count = data.totalCount;                  
						for(i=0;i<count;i++){
							var name = list[i].name;
							var url = list[i].url;
							var sort = list[i].sort;
							var state = list[i].state;
							var aid = list[i].auth_id;
							var child = '<input name="'+box_name+'"'+
                            ' type="checkbox" '+
                            ' value='+aid+' '+
                            ' id=auth_'+aid+' />'+name;
                            if((i+1)%4==0){
                            	child = child+' <br/>';
                            }
                          	$("#"+div_id).append(child);
						}
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
		}
    	
};

usermanager_onload = {
		begin : function(){
			var un = $.cookie("un");
			$("#username").val(un);
			user_manager.select_auth('auth_list','auth_box');
		}
};

